package cn.caplike.demo.google.protobuf.with_netty_multi;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Description: 客户端传递不同的消息, 服务端根据客户端的消息类型解析成不同的内容.
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2021-12-01 13:03
 */
public class TestServer {

    public static void main(String[] args) throws InterruptedException {
        // ~ 定义两个线程组, 其实就是两个死循环
        EventLoopGroup parentGroup = new NioEventLoopGroup(); // 不断的接收连接, 转给 childGroup
        EventLoopGroup childGroup = new NioEventLoopGroup(); // 真正完成对连接的处理

        try {
            // 启动服务端
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class)
                // ~ 子处理器, 真正的处理请求
                .childHandler(new TestServerChannelInitializer());

            final ChannelFuture channelFuture = serverBootstrap.bind(18899).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }
}
